package com.maple.tag;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.maple.hibernate.HibernateUtil;



public class OperationOfPage   {
	private Page page;
    public List  findSomeInfo(String className,Page page)
    {
    	className=" "+className+" ";
	    this.page=page;
	    
		List  list =  getSession().createQuery("from "+className).list();
        
		
		//数据库中记录的总条数
        int totalRowCount=list.size();
        page.setTotalRowCount(totalRowCount);
        
        //数据库中的数据总共分了多少页
        //设置pageSize
        if(page.getSize()<=0)
        {
        	page.setSize(PageTagConfig.INIT_SIZE);
        }
        //如果totalRowCount的不大于page.size ,totalRowCount可以为0
        if(totalRowCount/(page.getSize())==0)
        {
        	page.setPageCount(1);
        }
        //如果totalRowCount大于或等于page.size,并且是page.size的整数倍
        else if(totalRowCount%(page.getSize())==0)
        {
        	page.setPageCount(totalRowCount/page.getSize());
        }
        //如果totalRowCount大于page.size,并且不是page.size的整数倍
        else 
        {
        	page.setPageCount(totalRowCount/page.getSize()+1);
        }
        
        //处理page的currentIndex
        //设置currentIndex
        if(page.getCurrentIndex()<1)
        {
        	page.setCurrentIndex(1);
        }
        if(page.getCurrentIndex()>page.getPageCount())
        {
        	page.setCurrentIndex(page.getPageCount());
        }
        
        
        Query query = getSession().createQuery("from "+className);
        
        //TODO 这里加查询条件
        
        query.setFirstResult(page.getSize()*(page.getCurrentIndex()-1));
        query.setMaxResults(page.getSize());
        list = query.list();
        commit();
		return list;
    }
    
    
    
    private Session session=null;
	private void commit() {
		session.getTransaction().commit();
	}
	private Session getSession() {
		session=HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		return session;
	}
	public static <T> List<T> findSomeInfo(List<T> list,Page page)
	{
		
		//记录的总条数
        int totalRowCount=list.size();
        page.setTotalRowCount(totalRowCount);
        
        //数据库中的数据总共分了多少页
        //设置pageSize
        if(page.getSize()<=0)
        {
        	page.setSize(PageTagConfig.INIT_SIZE);
        }
        //如果totalRowCount的不大于page.size ,totalRowCount可以为0
        if(totalRowCount/(page.getSize())==0)
        {
        	page.setPageCount(1);
        }
        //如果totalRowCount大于或等于page.size,并且是page.size的整数倍
        else if(totalRowCount%(page.getSize())==0)
        {
        	page.setPageCount(totalRowCount/page.getSize());
        }
        //如果totalRowCount大于page.size,并且不是page.size的整数倍
        else 
        {
        	page.setPageCount(totalRowCount/page.getSize()+1);
        }
        
        //处理page的currentIndex
        //设置currentIndex
        if(page.getCurrentIndex()<1)
        {
        	page.setCurrentIndex(1);
        }
        if(page.getCurrentIndex()>page.getPageCount())
        {
        	page.setCurrentIndex(page.getPageCount());
        }
        int fromIndex=page.getSize()*(page.getCurrentIndex()-1);
        int toIndex;
		if((fromIndex+page.getSize())>page.getTotalRowCount())
		{
			toIndex=page.getTotalRowCount();
		}
        else
        {
        	toIndex=fromIndex+page.getSize();
        }
        return list.subList(fromIndex, toIndex);
	}
	
}
